package com.kh.first.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kh.first.utils.Tools;
import com.kh.first.utils.UserUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Component
public class LoginConfig implements HandlerInterceptor {

    List<String> excludeUrls;

        boolean use = false;
    {
        excludeUrls = new ArrayList<>();
        excludeUrls.add("loginController.do?goLogin");
        excludeUrls.add("loginController.do?doLogin");

    }


    //	<link rel="stylesheet" type="text/css" href="jquery-easyui-1.5.5.4/themes/default/easyui.css">
//	<link rel="stylesheet" type="text/css" href="jquery-easyui-1.5.5.4/themes/icon.css">
//	<script type="text/javascript" src="jquery-easyui-1.5.5.4/jquery.min.js"></script>
//	<script type="text/javascript" src="jquery-easyui-1.5.5.4/jquery.easyui.min.js"></script>
//	<script type="text/javascript" src="jquery-easyui-1.5.5.4/locale/easyui-lang-zh_CN.js"></script>
    String easyui_header = "<link rel=\"stylesheet\" type=\"text/css\" href=\"jquery-easyui-1.5.5.4/themes/material/easyui.css\">\n" +
            "<link rel=\"stylesheet\" type=\"text/css\" href=\"jquery-easyui-1.5.5.4/themes/icon.css\">\n" +
            "<script type=\"text/javascript\" src=\"jquery-easyui-1.5.5.4/jquery.min.js\"></script>\n" +
            "<script type=\"text/javascript\" src=\"jquery-easyui-1.5.5.4/jquery.easyui.min.js\"></script>\n" +
            "<script type=\"text/javascript\" src=\"jquery-easyui-1.5.5.4/locale/easyui-lang-zh_CN.js\"></script>\n" +
            "<script type=\"text/javascript\" src=\"tools/myTools.js\"></script>\n";
    String layui_header = "";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
//		String router = request.getServletPath();
        String router = Tools.getRequestPath(request);
//		request.setAttribute("easyui_header", easyui_header);
        request.setAttribute("layui_header", layui_header);
        String hohd = request.getParameter("hohd");

        HttpServletResponse res = (HttpServletResponse) response;

        String origin = request.getHeader("Origin");/*获取客户端的域名**/

        res.addHeader("Access-Control-Allow-Credentials", "true");/*允许带Cookie的跨域Ajax请求*/
        res.addHeader("Access-Control-Allow-Origin", origin); /*设置允许访问的域名地址**/
        res.addHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");/*允许请求的方式**/
        res.addHeader("Access-Control-Allow-Headers", "*");/*设置允许前端添加所有自定义请求头**/
//      res.addHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, X-File-Type, Cache-Control, Origin");

        /**
         * 设置预检命令的缓存时效。单位是"秒"
         * 如果没有失效，则不会再次发起OPTION预检请求
         */
        res.addHeader("Access-Control-Max-Age", "7200");
        if ("true".equals(hohd)) {
            return true;
        }
        if (!use) {
            return true;
        }
        if (excludeUrls.contains(router)) {
            return true;
        }
//        if (null == UserUtils.getUser()) {
//            response.sendRedirect("loginController.do?goLogin");
//            return false;
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
